home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / extensions / samples / xwindow.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  187 lines

  1. /*
  2.  * Copyright (c) 1995 Silicon Graphics, Inc.
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee,
  6.  * provided that (i) the above copyright notices and this permission
  7.  * notice appear in all copies of the software and related documentation,
  8.  * and (ii) the name of Silicon Graphics may not be used in any
  9.  * advertising or publicity relating to the software without the specific,
  10.  * prior written permission of Silicon Graphics.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  * 
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL,
  17.  * INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
  18.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19.  * OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  20.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  21.  * OF THIS SOFTWARE.
  22.  */
  23.  
  24. /*
  25.  * A few utility functions to for interfacing to X.
  26.  *
  27.  * $Revision: 1.1 $
  28.  *
  29.  */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <GL/glx.h>
  33. #include <GL/gl.h>
  34. #include "xwindow.h"
  35.  
  36. Bool
  37. waitForMapNotify(Display *dpy, XEvent *ev, XPointer arg)
  38. {
  39.     return (ev->type == MapNotify && ev->xmapping.window == *((Window *) arg));
  40. }
  41.  
  42. void
  43. createWindowAndContext(Display **dpy, Window *win, GLXContext *cx,
  44.                        int xpos, int ypos, int width, int height,
  45.                        GLboolean pref, XSizeHints *hints,
  46.                        int *visualAttr, 
  47.                        char *title)
  48. {
  49.     XSetWindowAttributes swa;
  50.     Colormap cmap;
  51.     XVisualInfo *vi;
  52.     int isRgba;
  53.     int i;
  54.  
  55.     /* get a connection */
  56.     *dpy = XOpenDisplay(NULL);
  57.     if (*dpy == NULL) {
  58.         fprintf(stderr, "Can't connect to display `%s'\n",
  59.                 getenv("DISPLAY"));
  60.     exit(EXIT_FAILURE);
  61.     }
  62.  
  63.     /* get an appropriate visual */
  64.     vi = glXChooseVisual(*dpy, DefaultScreen(*dpy), visualAttr);
  65.     if (vi == NULL) {
  66.         fprintf(stderr, "No matching visual on display `%s'\n",
  67.                 getenv("DISPLAY"));
  68.         exit(EXIT_FAILURE);
  69.     }
  70.     (void) glXGetConfig(*dpy, vi, GLX_RGBA, &isRgba);
  71.     /* create a GLX context */
  72.     if ((*cx = createContext(*dpy, vi)) == NULL) {
  73.     exit(EXIT_FAILURE);
  74.     }
  75.  
  76.     /* create a colormap (it's empty for rgba visuals) */
  77.     cmap = XCreateColormap(*dpy, RootWindow(*dpy, vi->screen),
  78.                            vi->visual, isRgba ? AllocNone : AllocAll);
  79.     if (cmap == NULL) {
  80.     fprintf(stderr,"Cannot create a colormap.\n");
  81.     exit(EXIT_FAILURE);
  82.     }
  83.  
  84.     /* fill the colormap with something simple */
  85.     if (!isRgba) {
  86.         static char *cnames[] = {
  87.             "black","red","green","yellow","blue","magenta","cyan","white"
  88.         };
  89.  
  90.     for (i = 0; i < sizeof(cnames)/sizeof(cnames[0]); i++) {
  91.         XStoreNamedColor(*dpy, cmap, cnames[i], i, DoRed|DoGreen|DoBlue);
  92.     }
  93.     }
  94.  
  95.     /* create a window */
  96.     swa.colormap = cmap;
  97.     swa.border_pixel = 0;
  98.     swa.event_mask = StructureNotifyMask | ButtonPressMask |
  99.         KeyPressMask | ExposureMask;
  100.     *win = XCreateWindow(*dpy, RootWindow(*dpy, vi->screen),
  101.                         xpos, ypos, width, height,
  102.                         0, vi->depth, InputOutput, vi->visual,
  103.                         CWBorderPixel | CWColormap | CWEventMask, &swa);
  104.     if (*win == NULL) {
  105.     fprintf(stderr,"Cannot create a window.\n");
  106.     exit(EXIT_FAILURE);
  107.     }
  108.  
  109.     XStoreName(*dpy, *win, title);
  110.  
  111.     /* handle "prefposition" style window */
  112.     if (pref) {
  113.     XSizeHints hints;
  114.  
  115.     hints.flags = USPosition|USSize; /*was PPosition|PSize which doesn't work*/
  116.     hints.x = xpos;
  117.     hints.y = ypos;
  118.     hints.width = width;
  119.     hints.height = height;
  120.  
  121.     XSetNormalHints(*dpy, *win,&hints);
  122.     } else if (hints != NULL)
  123.     XSetNormalHints(*dpy, *win, hints);
  124.  
  125.     mapWindowAndWait(*dpy, *win);
  126.  
  127.     /* Connect the context to the window */
  128.     if (!glXMakeCurrent(*dpy, *win, *cx)) {
  129.         fprintf(stderr, "Can't make window current to context\n");
  130.         exit(EXIT_FAILURE);
  131.     }
  132. }
  133.  
  134. void
  135. mapWindowAndWait(Display *dpy, Window win)
  136. {
  137.     XEvent event;
  138.  
  139.     XMapWindow(dpy, win);
  140.     XIfEvent(dpy, &event, waitForMapNotify, (XPointer) &win);
  141. }
  142.  
  143. /*
  144.  *  simple mouse button polling code for X
  145.  */
  146. int
  147. xGetButton(int button, Display *dpy, Window win)
  148. {
  149.     Window root,child;
  150.     int x,y;
  151.     unsigned int m;
  152.     unsigned int mask = 0;
  153.     int ok;
  154.  
  155.     switch (button) {
  156.     case 1:
  157.         mask = Button1Mask;
  158.         break;
  159.     case 2:
  160.         mask = Button2Mask;
  161.         break;
  162.     case 3:
  163.         mask = Button3Mask;
  164.         break;
  165.     }
  166.  
  167.     ok = XQueryPointer(dpy, win, &root, &child, &x, &y, &x, &y, &m);
  168.     
  169.     return ok && (m & mask);
  170. }
  171.  
  172.  
  173. /*
  174.  * Create a context and print stuff on failure.
  175.  */
  176. GLXContext
  177. createContext(Display *dpy, XVisualInfo *vi)
  178. {
  179.     GLXContext cx;
  180.     
  181.     /* create a GLX context */
  182.     if ((cx = glXCreateContext(dpy, vi, 0, GL_TRUE)) == NULL)
  183.     fprintf(stderr,"Cannot create a context.\n");
  184.     return cx;
  185. }
  186.  
  187.